package com.tech.response;

import java.io.Serializable;
import java.util.Date;
import com.tech.enumration.RespCodeEnum;
import com.tech.exception.BizException;

/**
 * @author admin
 * @Title BaseResponse
 * @Description 系统响应基类所有响应类继承该基类
 * @create 2017-11-02 下午5:17
 **/
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = -2633372191347041100L;
    /**
     * 返回信息状态码
     */
    // SUCCESS("0000", "成功"),
    // FAIL_SYSTEM("9999", "系统异常失败");
    // FAIL_BUSSINESS("9998", "业务异常失败");
    protected String code;
    /**
     * 返回信息
     */
    protected String msg;
    /**
     * 返回时间
     */
    // @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    // @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
    // @ApiModelProperty("服务器时间")
    private Date timestamp;

    private T data;

    public BaseResponse() {
    }

    public BaseResponse(String code, String msg, Date timestamp, T data) {
        this.code = code;
        this.msg = msg;
        this.timestamp = timestamp;
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Date getTimestamp() {
        if (timestamp == null) {
            timestamp = new Date();
        }
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    /**
     * @Title putSuccess
     * @Description 业务操作成功并返回对应数据
     * @param t
     * @since 2018年12月19日 上午9:33:28
     */
    public void putSuccess(T t) {
        this.code = RespCodeEnum.SUCCESS.getCode();
        this.msg = RespCodeEnum.SUCCESS.getMsg();
        this.data = t;
    }

    /**
     * @Title putFail
     * @Description 未知异常导致的失败
     * @param msg
     * @since 2018年12月19日 上午9:26:41
     */
    public void putFail(String msg) {
        this.code = RespCodeEnum.FAIL_SYSTEM.getCode();
        this.msg = msg;
    }

    /**
     * @Title putFailBussiness
     * @Description 具体异常导致的失败
     * @param e
     * @since 2018年12月19日 上午9:26:56
     */
    public void putFailBussiness(BizException e) {
        this.code = e.getDefineCode();
        this.msg = e.getMessage();
    }
}
